const { Sequelize, DataTypes, Model } = require("sequelize");
const start = require("./utils/connect");

(async () => {
  const sequelize = await start();
  const Product = sequelize.define("product", {
    title: {
      type: DataTypes.STRING
    },
    uuid: {
      type: DataTypes.UUID,
      defaultValue: Sequelize.UUIDV1
    },
  }, {
    freezeTableName: true // 强制使用定义表名
  });

  const User = sequelize.define("user", {
    firstname: {
      type: DataTypes.STRING
    },
    lastname:{
      type:DataTypes.STRING
    },
    age: {
      type: DataTypes.INTEGER,
      defaultValue: 24
    },
    uuid: {
      type: DataTypes.UUID,
      defaultValue: Sequelize.UUIDV4
    }
  }, {
    freezeTableName: true // 强制使用定义表名
  });

  const Address = sequelize.define("address",{
    type:DataTypes.STRING,
    line1:Sequelize.STRING,
    line2:Sequelize.STRING,
    city:Sequelize.STRING,
    state:Sequelize.STRING,
    zip:Sequelize.STRING
  },{
    freezeTableName:true
  });

  // class Address extends Model{}

  // await Address.init({
  //   type:DataTypes.STRING,
  //   line1:Sequelize.STRING,
  //   line2:Sequelize.STRING,
  //   city:Sequelize.STRING,
  //   state:Sequelize.STRING,
  //   zip:Sequelize.STRING
  // },{sequelize,modelName:"address"});


  Product.User = Product.belongsTo(User);
  User.Addresses = User.hasMany(Address);

  // await User.sync({
  //   alter: true,
  // });

  // await Address.sync({
  //   alter:true
  // });

  // await Product.sync({
  //   alter: true,
  // });
  
  await sequelize.sync({
    alter:true
  });
  

  await Product.create({
    title:"Chair",
    user:{
      firstname:"Mick",
      lastname:"Broadstone",
      addresses:[{
        type:"home",
        line1:"100 Main St.",
        city:"Austin",
        state:"TX"
      },{
        type:"compayn",
        line1:"100 Main St.",
        city:"Zhengzhou",
        state:"HN"
      }]
    }
  },{
    include:[{
      association:Product.User,
      include:[User.Addresses]
    }]
  })

  


})();